package com.example.completionservice.aatest2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;

public class QuickTaskResultHandler<T, R> {
    private static final Logger logger = LoggerFactory.getLogger(QuickTaskResultHandler.class);

//    //1.定义执行业务任务浅程池
//     ExecutorService executorService = Executors.newFixedThreadPool(12);
//
//     final QuickProcessByNotify<String, Long>
//            quickProcess = new QuickProcessByNotify<String, Long>(executorService, new QuickTaskResultHandler<String, Long>());//{

//    QuickProcessByNotify<String, Long> quickProcess = null;
//    public QuickTaskResultHandler(){
//        quickProcess = new QuickProcessByNotify<String, Long>(executorService, new QuickTaskResultHandler<String, Long>());//{
//    }




    public void handle(Future<T> future) {
        T t = null;
        try {
            successCount.incrementAndGet();
            t = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        //todo 这里放业务汇总逻辑
        logger.info("这里放业务汇总逻辑: " + t);
    }

    //业务结果汇总处理类,这里最终汇总结果是long类型的成功数successCount
    private AtomicLong successCount = new AtomicLong(0);
    public Long getResult() {
        //返回业务汇总结果
        return successCount.get();
    }


//    //3.调用processByCallBack方法，开始查询业务数据并处理
//    long allCount = quickProcess.processByCallBack(
//            new QuickBizProcessCallbak() {
//                //QuickBizProcessCallbak是业务处理回调接口，业务方实现此接口
//                public void doBiz() {
//                    quickProcess.submit(
//                       getTask()
//                    );
//                }
//            }
//    );
//
//    // 得到一个任务
//    public Callable<String> getTask() {
//        // final Random rand = new Random();
//        Callable<String> task = new Callable<String>() {
//            //一定要提交业务任务到快速处理器
//            public String call() throws Exception {
////              return "success";
//                int i = 1;
//                int j = 0;
//                int sum = i + j;
//                //logger.info(sum + "\t");
//                return sum + "";
//            }
//        };
//        return task;
//    }

}
